package com.xxg.oauth2.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/login")
public class Login {

    @Qualifier("getTokenStore")
    @Autowired
    TokenStore tokenStore;

    @RequestMapping("/logout")
    public ResponseEntity login(HttpServletRequest request, HttpServletResponse response) {
        String header = request.getHeader("Authorization");
        if(header != null) {
            String tokenV = header.replace("bearer", "").trim();
            OAuth2AccessToken auth2AccessToken = tokenStore.readAccessToken(tokenV);
            tokenStore.removeAccessToken(auth2AccessToken);
        }
        return ResponseEntity.ok(200);
    }
}
